/*
 * ModelCC, distributed under ModelCC Shared Software License, www.modelcc.org
 */

package org.modelcc.language.syntax.builder;

import java.io.Serializable;
import java.util.logging.Level;

import org.modelcc.language.factory.SymbolIdentifier;
import org.modelcc.language.syntax.ParserMetadata;
import org.modelcc.language.syntax.Symbol;
import org.modelcc.language.syntax.SymbolBuilder;

/**
 * Decorator symbol builder
 * 
 * @author Luis Quesada (lquesada@modelcc.org) & Fernando Berzal (fberzal@modelcc.org)
 */
public final class DecoratorSymbolBuilder extends SymbolBuilder implements Serializable 
{
    /**
     * Builds a symbol, filling its data, and validates it.
     * @param t symbol to be built.
     * @param data the parser data.
     * @return true if the symbol is valid, false if not
     */
    @Override
	public boolean build(Symbol t, ParserMetadata data) 
    {
        SymbolIdentifier eid = (SymbolIdentifier)t.getType();
        eid.getElement();
        Object o = null;
        boolean valid = true;
        int target = -1;
        for (int i = 0;i < t.size();i++) {
            if (t.getContent(i).getType().getClass().equals(SymbolIdentifier.class))
                target = i;
        }

        try {
            if (target != -1) {
                o = t.getContent(target).getUserData();
            }

            t.setUserData(o);

        } catch (Exception ex) {
            log(Level.SEVERE, null, ex);
            return false;
        }
        return valid;
    }
}
